package com.anolesoft.twelvePlan.controller;

import cn.hutool.core.bean.BeanUtil;
import com.anolesoft.common.core.domain.LoginUser;
import com.anolesoft.common.core.web.domain.AjaxResult;
import com.anolesoft.twelvePlan.dto.MesGyInfoToPlanDTO;
import com.anolesoft.twelvePlan.service.MesPlanXiaDaService;
import com.anolesoft.twelvePlan.vo.MesGyInfoToPlanVO;
import com.anolesoft.yr.demo.PageResult;
import com.anolesoft.yr.util.PageData;
import com.anolesoft.yr.util.SecurityUtils;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;
import java.util.stream.Collectors;

/**
 * @author zyq
 * @date 2022/3/18
 */
@RestController
@RequestMapping(value="/xiada")
@Slf4j
public class MesPlanXiaDaController {
    @Autowired
    private MesPlanXiaDaService mesPlanXiaDaService;

    @PostMapping(value = "/factoryId")
    public AjaxResult factoryId() {
        LoginUser loginUser = SecurityUtils.getLoginUser();
        String factoryId = loginUser.getFacCode();
        log.debug("厂代号：{}" + factoryId);
        return AjaxResult.success(factoryId);
    }

    @PostMapping(value = "/query")
    public AjaxResult queryPlanInfo(@RequestBody MesGyInfoToPlanVO mesGyInfoToPlanVO){
        log.debug("mesGyInfoToPlanVO：{}", mesGyInfoToPlanVO);
        LoginUser loginUser = SecurityUtils.getLoginUser();
        mesGyInfoToPlanVO.setFactoryId(loginUser.getFacCode());
        Integer pageSize = mesGyInfoToPlanVO.getPageSize();
        Integer pageNo = mesGyInfoToPlanVO.getPageNo();
        MesGyInfoToPlanDTO mesGyInfoToPlanDTO = BeanUtil.copyProperties(mesGyInfoToPlanVO, MesGyInfoToPlanDTO.class);
        log.debug("mesGyInfoToPlanDTO：{}", mesGyInfoToPlanDTO);
        PageData<List<MesGyInfoToPlanDTO>> listPageData = this.mesPlanXiaDaService.queryPlan(mesGyInfoToPlanDTO, pageNo, pageSize);
        if (listPageData == null) {
            return AjaxResult.success("该机型导入后还没有进行信息匹配，请等到明日信息匹配完成后再进行查询或联系管理员。");
        }
        List<MesGyInfoToPlanVO> mesGyInfoToPlanVOS = listPageData.getItems().stream()
                .map(t -> BeanUtil.copyProperties(t, MesGyInfoToPlanVO.class))
                .collect(Collectors.toList());
        PageResult pageResult = new PageResult(mesGyInfoToPlanVOS, pageSize, pageNo, listPageData.getTotal());
        return AjaxResult.success("查询成功", pageResult);

    }

    //@PostMapping(value = "/xiada")
    //public AjaxResult planXiaDa(@RequestBody String[] planIds){
    //    //log.debug("planIds：" + planIds);
    //    for(String s : planIds){
    //        System.out.println(s);
    //    }
    //    LoginUser loginUser = SecurityUtils.getLoginUser();
    //    String factoryId = loginUser.getFacCode();
    //    String user = loginUser.getEmpUid();
    //    log.debug("factoryId：" + factoryId + "user：" + user);
    //    String result = mesPlanXiaDaService.planXiaDa(planIds, factoryId, user);
    //    return AjaxResult.success(result);
    //}
    @PostMapping(value = "/xiada")
    public AjaxResult planXiaDa(@RequestBody List<MesGyInfoToPlanVO> mesGyInfoToPlanVOS){
        for(MesGyInfoToPlanVO mesGyInfoToPlanVO : mesGyInfoToPlanVOS){
            log.debug("mesGyInfoToPlanVO：{}", mesGyInfoToPlanVO);
        }
        LoginUser loginUser = SecurityUtils.getLoginUser();
        String factoryId = loginUser.getFacCode();
        String user = loginUser.getEmpUid();
        String result = mesPlanXiaDaService.planXiaDa(mesGyInfoToPlanVOS, factoryId, user);
        return AjaxResult.success(result);
    }

    @PostMapping(value = "/xiaDaForJx")
    public AjaxResult xiaDaForJx (@RequestBody MesGyInfoToPlanVO mesGyInfoToPlanVO) {
        log.debug("mesGyInfoToPlanVO：{}", mesGyInfoToPlanVO);
        MesGyInfoToPlanDTO mesGyInfoToPlanDTO = BeanUtil.copyProperties(mesGyInfoToPlanVO, MesGyInfoToPlanDTO.class);
        LoginUser loginUser = SecurityUtils.getLoginUser();
        mesGyInfoToPlanDTO.setFactoryId(loginUser.getFacCode());
        mesGyInfoToPlanDTO.setXiadauid(loginUser.getEmpUid());
        log.debug("mesGyInfoToPlanDTO：{}", mesGyInfoToPlanDTO);
        int num = mesPlanXiaDaService.xiaDaForJx(mesGyInfoToPlanDTO);
        StringBuffer result = new StringBuffer();
        if(num == 0) {
            result.append("该月计划已经全部下达过！");
        } else {
            result.append("下达成功！共下达");
            result.append(num);
            result.append("条计划");
        }
        log.debug(result.toString());
        return AjaxResult.success(result.toString());
    }

    @PostMapping(value = "/update")
    public AjaxResult updateCheckTime(@RequestBody MesGyInfoToPlanVO mesGyInfoToPlanVO){
        log.debug("mesGyInfoToPlanVO：{}", mesGyInfoToPlanVO);
        MesGyInfoToPlanDTO mesGyInfoToPlanDTO = BeanUtil.copyProperties(mesGyInfoToPlanVO, MesGyInfoToPlanDTO.class);
        log.debug("mesGyInfoToPlanDTO：{}", mesGyInfoToPlanDTO);
        if (mesPlanXiaDaService.updateCheckTime(mesGyInfoToPlanDTO) == 1) {
            return AjaxResult.success("填写成功！");
        } else {
            return AjaxResult.success("填写失败，请重试");
        }
    }

}
